# -*-coding: utf-8 -*-
"""
    @Theme   : 自定义 flask 命令
    @Time    : 2024/6/9 11:16
    @Author  : YamFish
    @Url     : https://www.zhihu.com/people/yamfish
"""
from datetime import datetime

import click
from faker import Faker

from . import app, db

fake = Faker('zh_CN')


@app.cli.command()
@click.option('--count', default=10, help='The num of students, default is 10.')
def forge(count):
    """Generate MongoDB Records"""
    start_id = db._query_last_id() + 1
    students = []
    for _ in range(count):
        birthday = fake.date_between(datetime(1970, 1, 1), datetime(2008, 1, 1))
        today = datetime.now()
        age = today.year - birthday.year
        if today.month < birthday.month or (today.month == birthday.month and today.day < birthday.day):
            age -= 1
        students.append({
            'id': start_id,
            'name': fake.name(),
            'age': age,
            'birthday': birthday.strftime('%Y-%m-%d'),
            'current_home': fake.province(),
            'origin_home': fake.province(),
            'deleted': 0
        })
        start_id += 1
    print(db.collection.insert_many(students))
